/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.boxin.ims.modules.ad.web;

import java.io.File;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.utils.FileUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import com.boxin.ims.modules.ad.entity.Advertisement;
import com.boxin.ims.modules.ad.service.AdvertisementService;
import com.boxin.ims.modules.wechat.utils.WechatUtils;

/**
 * 广告Controller
 * @author Jakemanse
 * @version 2014-04-18
 */
@Controller
@RequestMapping(value = Global.ADMIN_PATH+"/ad/advertisement")
public class AdvertisementController extends BaseController {

	@Autowired
	private AdvertisementService advertisementService;
	
	@ModelAttribute
	public Advertisement get(@RequestParam(required=false) Long id) {
		if (id != null){
			return advertisementService.get(id);
		}else{
			return new Advertisement();
		}
	}
	
	@RequiresPermissions("ad:advertisement:view")
	@RequestMapping(value = {"list", ""})
	public String list(Advertisement advertisement, HttpServletRequest request, HttpServletResponse response, Model model) {
		User user = UserUtils.getUser();
		if (!user.isAdmin()){
			advertisement.setUser(user);
		}
        Page<Advertisement> page = advertisementService.find(new Page<Advertisement>(request, response), advertisement); 
        model.addAttribute("page", page);
		return "modules/ad/advertisementList";
	}

	@RequiresPermissions("ad:advertisement:view")
	@RequestMapping(value = "form")
	public String form(Advertisement advertisement, Model model) {
	
		User user = UserUtils.getUser();
		if (advertisement.getUser() == null || advertisement.getUser().getId() <1){
			advertisement.setUser(user);
		}
		
		model.addAttribute("advertisement", advertisement);
		return "modules/ad/advertisementForm";
	}

	@RequiresPermissions("ad:advertisement:edit")
	@RequestMapping(value = "save")
	public String save(@RequestParam("picfile") MultipartFile picfile,Advertisement advertisement, Model model,HttpServletRequest request, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, advertisement)){
			return form(advertisement, model);
		}
		
		String startup=request.getParameter("startup");
		if("0".equals(startup)){
			advertisement.setStartup(false);
		}
		if("1".equals(startup)){
			advertisement.setStartup(true);
		}
		
		//上传图片
		if(picfile != null && picfile.getOriginalFilename().length()>4){
			String advertPath = WechatUtils.getWechatAdvertResourceSavePath(request)+File.separator;
			FileUtils.createDirectory(advertPath);
			String sname = picfile.getOriginalFilename();
			String fileType = sname.substring(sname.lastIndexOf("."));
			String fname  = UserUtils.getUser().getLoginName()+	"_ad_"+System.currentTimeMillis()+fileType;
			File file = new File(advertPath+fname);
			System.out.println("保存文件:"+advertPath+fname);
			try {
				picfile.transferTo(file);
				advertisement.setPicName(file.getName());
			} catch (Exception e) {
				System.out.println("上传文件失败:"+advertPath+fname);
				e.printStackTrace();
			}
			
		}
		
		
		advertisementService.save(advertisement);
		addMessage(redirectAttributes, "保存广告'" + advertisement.getName() + "'成功");
		return "redirect:"+Global.ADMIN_PATH+"/ad/advertisement/?repage";
	}
	
	@RequiresPermissions("ad:advertisement:edit")
	@RequestMapping(value = "delete")
	public String delete(Long id, RedirectAttributes redirectAttributes) {
		advertisementService.delete(id);
		addMessage(redirectAttributes, "删除广告成功");
		return "redirect:"+Global.ADMIN_PATH+"/ad/advertisement/?repage";
	}

}
